<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth,Hash;
header('Content-Type:text/html;charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
class ApiController extends Controller
{

    public function login(request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            if (empty($data->username)) {
                $result = array('status' => '1001', 'message' => '请输入用户名');
                echo json_encode($result,TRUE);
                exit();
            }
            if (empty($data->password)) {
                $result = array('status' => '1002', 'message' => '请输入密码');
                echo json_encode($result,TRUE);
                exit();
            }

            $map['email']= $data->username;
            $map['role_id'] = '15';
            $password = db::table('admin_users')->where($map)->value('password');
            if(!Hash::check($data->password, $password)){
                $result = array('status' => '1003',  'message' => '请输入正确的用户名或密码' );
                echo json_encode($result,TRUE);
                exit();
            }else{
                $re = db::table('admin_users')->where($map)->first();
                //获取上次登陆时间
                if($re->login_time){
                    $re->last_login_time = $re->login_time;
                }else{
                    $re->last_login_time = date('Y-m-d H:i:s',time());
                }
                //记录本次登陆时间
                $date['login_time'] = date('Y-m-d H:i:s',time());
                db::table('admin_users')->where('id',$re->id)->update($date);

                $re->paichusuo = getValue($re->org_id,'area' ,'id' ,'name' );
                $where = array(
                    'org_id' => $re->org_id,
                    'minjing' => $re->id,
                    'is_del' => '0',
                );
                //单位
                $lists = DB::table('units')->where($where)->orderby('is_imp','desc')->take(5)->get();
                foreach ($lists as $k=>$v){
                    $v->minjing_name = getValue($v->minjing,'admin_users' ,'id' , 'name');
                    $v->type = getValue($v->type,'unit_type' ,'id' , 'name');
                    if($v->is_imp == 1){
                        $v->is_imp = '省级';
                    }elseif($v->is_imp == 2){
                        $v->is_imp = '市级';
                    }elseif($v->is_imp == 3){
                        $v->is_imp = '县级';
                    }elseif($v->is_imp == 4){
                        $v->is_imp = '乡级';
                    }else{
                        $v->is_imp = '非重点';
                    }
                }
                $result = array('status' => '1000',  'message' => '登录成功' ,'info'=>$re,'lists'=>$lists);
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function company_list(Request $request){
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $id = $data->id;
            $re = DB::table('units')->where('minjing',$data->id)->get();
            foreach ($re as $k=>$v){
                $v->minjing = getValue($v->minjing,'admin_users' ,'id' , 'name');
                $v->type_id = $v->type;
                $v->type = getValue($v->type,'unit_type' ,'id' , 'name');
                $v->shi = getValue($v->city,'area' ,'id' ,'name' );
                $v->qu = getValue($v->area,'area' ,'id' ,'name' );
                $v->paichusuo = getValue($v->org_id,'area' ,'id' ,'name' );
                if($v->is_imp == 1){
                    $v->is_imp = '省级';
                }elseif($v->is_imp == 2){
                    $v->is_imp = '市级';
                }elseif($v->is_imp == 3){
                    $v->is_imp = '县级';
                }elseif($v->is_imp == 4){
                    $v->is_imp = '乡级';
                }else{
                    $v->is_imp = '非重点';
                }
            }
            if($re){
                $result = array('status' => '1000',  'message' => '' ,'info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到信息' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function company_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $id = $data->id;
            $re = DB::table('units')->where('id',$id)->first();
            if($re){
                $re->minjing = getValue($re->minjing,'admin_users' ,'id' , 'name');
                $re->type_name = getValue($re->type,'unit_type' ,'id' , 'name');
                $re->shi = getValue($re->city,'area' ,'id' ,'name' );
                $re->qu = getValue($re->area,'area' ,'id' ,'name' );
                $re->paichusuo = getValue($re->org_id,'area' ,'id' ,'name' );
                if($re->is_imp == 10){
                    $re->is_imp = '省级重点单位';
                }elseif($re->is_imp == 20){
                    $re->is_imp = '市级重点单位';
                }elseif($re->is_imp == 30){
                    $re->is_imp = '县区级重点单位';
                }elseif($re->is_imp == 40){
                    $re->is_imp = '一般单位';
                }

                if($re->xingzhi == 10){
                    $re->xingzhi = '国有';
                }elseif($re->xingzhi == 20){
                    $re->xingzhi = '民营';
                }elseif($re->xingzhi == 30){
                    $re->xingzhi = '股份';
                }elseif($re->xingzhi == 40){
                    $re->xingzhi = '合资';
                }elseif($re->xingzhi == 50){
                    $re->xingzhi = '外资';
                }elseif($re->xingzhi == 60){
                    $re->xingzhi = '个体';
                }

                if($re->is_del == 0){
                    $re->is_del = '实有';
                }elseif($re->is_del == 1){
                    $re->is_del = '注销';
                }elseif($re->is_del == 2){
                    $re->is_del = '合并';
                }

                //要害部位数量
                $re->buwei_num = db::table('zhongyaobuwei')->where('unit_id',$id)->count();
                //重点岗位人数
                $re->zdgw_num = db::table('zhongdianrenyuan')->where('unit_id',$id)->count();
                //在职保卫人数
                $re->zaizhi_num = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','在职职工')->count();
                //自招保安人数
                $re->zizhao_num = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','自招')->count();
                //外聘保安人数
                $re->waipin_num= db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','外聘')->count();

                //行业字段
                $field_count = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->count();
                if($field_count > 0){
                    $field = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->orderby('paixu','asc')->get();
                    foreach ($field as $k=>$v){
                        $v->val = db::table('unit_type_field_value')->where('field_id',$v->id)->where('unit_id',$id)->value('val');
                        /*if($v->field_type == 2){
                            $v->daan = db::table('unit_type_field_daan')->where('pid',$v->id)->get();
                        }*/
                    }
                }else{
                    $field = array();
                }

                $result = array('status' => '1000',  'message' => '' ,'info'=>$re ,'field'=>$field);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到信息' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function company_edit(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $id = $res->id;
            $username = $res->username;
            $data['user_id'] = getValue($username,'company' ,'username' , 'id');
            $data[$res->datatype] = $res->modifydata;
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            /*$result = array('status' => '1001',  'info' => $data );
            echo json_encode($result,TRUE);
            exit();*/

            $re = DB::table('units')->where('id',$id)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '修改成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '修改失败' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function baoweirenyuan_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $unit_id = $data->id;

            $re = DB::table('baoweirenyuan')->where('unit_id',$unit_id)->get();
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function baoweirenyuan_edit(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $id = $res->id;
            $username = $res->username;
            $data['user_id'] = getValue($username,'company' ,'username' , 'id');
            $data[$res->datatype] = $res->bwryxx;
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            /*$result = array('info' => $data );
            echo json_encode($result,TRUE);
            exit();*/

            $re = DB::table('baoweirenyuan')->where('id',$id)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '修改成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '修改失败' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function baoweirenyuan_add(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $username = $res->username;
            $data['unit_id'] = $res->unit_id;
            $data['user_id'] = getValue($username,'company' ,'username' , 'id');
            $data['name'] = $res->name;
            $data['sex'] = $res->sex;
            $data['age'] = $res->age;
            $data['education'] = $res->education;
            $data['political_status'] = $res->political_status;
            $data['position'] = $res->position;
            $data['native_place'] = $res->native_place;
            $data['address'] = $res->address;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            /*$result = array('info' => $data );
            echo json_encode($result,TRUE);
            exit();*/

            $re = DB::table('baoweirenyuan')->insert($data);
            if($re){
                $unit_id = $res->unit_id;

                $list = DB::table('baoweirenyuan')->where('unit_id',$unit_id)->get();

                $result = array('status' => '1000',  'message' => '添加成功','info' => $list);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '添加失败' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function baoweirenyuan_del(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $username = $res->username;
            $data['unit_id'] = $res->unit_id;
            $data['user_id'] = getValue($username,'company' ,'username' , 'id');
            $map['id'] = $res->id;

            /*$result = array('info' => $data );
            echo json_encode($result,TRUE);
            exit();*/

            $re = DB::table('baoweirenyuan')->where($map)->delete();
            if($re){
                $result = array('status' => '1000',  'message' => '删除成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '删除失败' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }


    //线索举报
     public function xsqy_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::select("SELECT units.id,units.name,units.pic,units.address FROM `units` JOIN xiansuo ON  units.id = xiansuo.unit_id  WHERE units.minjing = $data->mjid GROUP BY units.id,units.name,units.pic,units.address");
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

     public function xs_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $re = DB::table('xiansuo')->where('unit_id',$data->qyid)->get();
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function xs_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('xiansuo')->where('xs_id',$data->jqid)->first();
            $unit_info = DB::table('units')->where('id',$re->unit_id)->first();
            $re->city = getValue($unit_info->city,'area' ,'id' ,'name' );
            $re->area = getValue($unit_info->area,'area' ,'id' ,'name' );
            $re->org = getValue($unit_info->org_id,'area' ,'id' ,'name' );
            $re->unit_name = $unit_info->name;
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function xs_chuli(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $res->dt->update_at = date('Y-m-d H:i:s',time());
            $res->dt->police_id = $res->mjid;
            $res->dt->police_cl = '1';
            foreach ($res->dt as $k=>$v){
                $data[$k] = $v;
            }
            $jq_info = DB::table('xiansuo')->where('xs_id',$res->jqid)->first();
            $data['clsj'] = date('Y-m-d H:i:s',time());
            $data['chuli_time'] = time()-strtotime($jq_info->created_at);
            $re = DB::table('xiansuo')->where('xs_id',$res->jqid)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }


    //意见反馈
     public function yjqy_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::select("SELECT units.id,units.name,units.pic,units.address FROM `units` JOIN yjfk ON  units.id = yjfk.unit_id  WHERE units.minjing = $data->mjid GROUP BY units.id,units.name,units.pic,units.address");
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

     public function yj_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $re = DB::table('yjfk')->where('unit_id',$data->qyid)->get();
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function yj_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('yjfk')->where('yj_id',$data->jqid)->first();
            $unit_info = DB::table('units')->where('id',$re->unit_id)->first();
            $re->city = getValue($unit_info->city,'area' ,'id' ,'name' );
            $re->area = getValue($unit_info->area,'area' ,'id' ,'name' );
            $re->org = getValue($unit_info->org_id,'area' ,'id' ,'name' );
            $re->unit_name = $unit_info->name;
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function yj_chuli(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $res->dt->update_at = date('Y-m-d H:i:s',time());
            $res->dt->police_id = $res->mjid;
            $res->dt->police_cl = '1';
            foreach ($res->dt as $k=>$v){
                $data[$k] = $v;
            }
            $re = DB::table('yjfk')->where('yj_id',$res->jqid)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }









    //有警情的企业列表
    public function qiye_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $re = DB::select("SELECT units.id,units.name,units.pic,units.address FROM `units` JOIN jqsb ON  units.id = jqsb.unit_id  WHERE units.minjing = $data->mjid GROUP BY units.id,units.name,units.pic,units.address");
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function jq_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('jqsb')->where('unit_id',$data->qyid)->get();
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function jq_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('jqsb')->where('jq_id',$data->jqid)->first();
            $unit_info = DB::table('units')->where('id',$re->unit_id)->first();
            $re->city = getValue($unit_info->city,'area' ,'id' ,'name' );
            $re->area = getValue($unit_info->area,'area' ,'id' ,'name' );
            $re->org = getValue($unit_info->org_id,'area' ,'id' ,'name' );
            $re->unit_name = $unit_info->name;
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function jq_chuli(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $res->dt->update_at = date('Y-m-d H:i:s',time());
            $res->dt->police_id = $res->mjid;
            $res->dt->police_cl = '1';
            foreach ($res->dt as $k=>$v){
                $data[$k] = $v;
            }

            $jq_info = DB::table('jqsb')->where('jq_id',$res->jqid)->first();
            $data['czsj'] = date('Y-m-d H:i:s',time());
            $data['chuli_time'] = time()-strtotime($jq_info->created_at);
            $re = DB::table('jqsb')->where('jq_id',$res->jqid)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    //有人员核查的企业列表
    public function qiye_list2(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::select("SELECT units.id,units.name,units.pic,units.address FROM `units` JOIN rysh ON  units.id = rysh.unit_id  WHERE units.minjing = $data->mjid GROUP BY units.id,units.name,units.pic,units.address");
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function rysh_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('rysh')->where('unit_id',$data->qyid)->get();
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function rysh_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('rysh')->where('rysh_id',$data->rysh_id)->first();
            $unit_info = DB::table('units')->where('id',$re->unit_id)->first();
            $re->city = getValue($unit_info->city,'area' ,'id' ,'name' );
            $re->area = getValue($unit_info->area,'area' ,'id' ,'name' );
            $re->org = getValue($unit_info->org_id,'area' ,'id' ,'name' );
            $re->unit_name = $unit_info->name;
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function ry_shenhe(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $res->dt->update_at = date('Y-m-d H:i:s',time());
            $res->dt->police_id = $res->mjid;
            $res->dt->police_cl = '1';
            foreach ($res->dt as $k=>$v){
                $data[$k] = $v;
            }

            $jq_info = DB::table('rysh')->where('rysh_id',$res->rysh_id)->first();
            $data['clsj'] = date('Y-m-d H:i:s',time());
            $data['chuli_time'] = time()-strtotime($jq_info->created_at);
            $re = DB::table('rysh')->where('rysh_id',$res->rysh_id)->update($data);
            if($re){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function member_info(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            //企业数量
            $res['qiye_num'] = DB::table('units')->where('minjing',$data->mjid)->count();
            //警情数量
            $unit_id = DB::table('units')->where('minjing',$data->mjid)->pluck('id');

            $res['jinqing_num'] = DB::table('jqsb')->whereIn('unit_id',$unit_id)->count();
            $res['ycljq_num'] = DB::table('jqsb')->where('police_cl','1')->whereIn('unit_id',$unit_id)->count();
            $res['wcljq_num'] = DB::table('jqsb')->where('police_cl','0')->whereIn('unit_id',$unit_id)->count();

            //人员核查数量
            $res['hecha_num'] = DB::table('rysh')->whereIn('unit_id',$unit_id)->count();
            $res['yhc_num'] = DB::table('rysh')->where('police_cl','1')->whereIn('unit_id',$unit_id)->count();
            $res['whc_num'] = DB::table('rysh')->where('police_cl','0')->whereIn('unit_id',$unit_id)->count();

            //自己检查数量
            $res['self'] = db::table('jiancha')->where('user_id',$data->mjid)->count();//自己检查数量

            //别人检查数量
            $res['other'] = db::table('jiancha')->where('mj_id',$data->mjid)->where('is_self','2')->count();//别人检查数量

            //通知数量
            $res['tz_num'] = DB::table('tongzhi')->count();

            //信息待审核企业数量
            $map['police_id'] = $data->mjid;
            $map['is_pass1'] = '1';
            $res['daishenhe_num'] = DB::table('units_update')->where($map)->count();

            $res['yishenhe_num'] = DB::table('units_update')->where('police_id',$data->mjid)->whereIn('is_pass1',[2,3])->count();

            if($res){
                $result = array('status' => '1000',  'message' => $res);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '暂无数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }


    public function tongzhi_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('tongzhi')->orderby('tz_id','desc')->get();
            foreach($re as $k=>$v){
                $v->fuburen = getValue($v->uid,'admin_users' ,'id' ,'name' );
            }
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function tongzhi_detail(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('tongzhi')->where('tz_id',$data->tz_id)->first();
            $re->fuburen = getValue($re->uid,'admin_users' ,'id' ,'name' );
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function bangzhu_list(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            $map['type'] = 2;
            $re = DB::table('bz')->where($map)->orderby('id','desc')->get();
            foreach($re as $k=>$v){
                $v->fuburen = getValue($v->user_id,'admin_users' ,'id' ,'name' );
            }
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function bangzhu_detail(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));

            $re = DB::table('bz')->where('id',$data->bz_id)->first();
            $re->fuburen = getValue($re->user_id,'admin_users' ,'id' ,'name' );
            if($re){
                $result = array('status' => '1000','info'=>$re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function search_qiye(Request $request)
    {
        if($request->isMethod('post')){
            $data = json_decode(file_get_contents("php://input"));
            if($data->type == 0){
                if(!$data->name){
                    $map['minjing'] = $data->mjid;
                }else{
                    $map['minjing'] = $data->mjid;
                    $map[] = array('name','like','%'.$data->name.'%');
                }
            }
            if($data->type == 1){
                if(!$data->name){
                    $result = array('status' => '1001',  'message' => '请输入关键字' );
                    echo json_encode($result,TRUE);
                    exit();
                }else{
                    $map[] = array('name','like','%'.$data->name.'%');
                }
            }

            $re = DB::table('units')->where($map)->get();
            $list = array();
            if($re){
                foreach ($re as $k=>$v){
                    $list[$k]['id'] = $v->id;
                    $list[$k]['name'] = $v->name;
                    $list[$k]['pic'] = "http://nb.yntosite.com/".$v->pic;
                    $list[$k]['address'] = getValue($v->city,'area' ,'id' ,'name' ).getValue($v->area,'area' ,'id' ,'name' ).getValue($v->org_id,'area' ,'id' ,'name' ).$v->address;
                }
                $result = array('status' => '1000','info'=>$list);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1002',  'message' => '未查询到数据' );
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function add_jiancha(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            //安全检查项
            $qiye_type = getValue($res->qyid,'units' ,'id' ,'type' );
            $where['type_id'] = $qiye_type;
            $where['aqjc_type'] = $res->jc_type;
            $aqjc_count = db::table('unit_type_aqjc')->where($where)->where('is_show','1')->count();
            if($aqjc_count > 0){
                $aqjc = db::table('unit_type_aqjc')->where($where)->where('is_show','1')->orderby('paixu','asc')->get();
                foreach ($aqjc as $k=>$v){
                    if($v->field_type == 2){
                        $v->daan = db::table('unit_type_aqjc_daan')->where('pid',$v->id)->get();
                    }
                }
            }else{
                $aqjc = array();
            }

            if($aqjc){
                $result = array('status' => '1000',  'info' => $aqjc);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function save_jiancha(Request $request)
    {
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $jiancha = $res->data;

            $unit_info = db::table('units')->where('id',$res->qyid)->first();
            if($unit_info->org_id){
                $data['city'] = $unit_info->city;
                $data['area'] = $unit_info->area;
                $data['org_id'] = $unit_info->org_id;
                $data['unit_name'] = $unit_info->name;
            }else{
                $result = array('status' => '1001',  'message' => '请先完善单位市区派出所信息');
                echo json_encode($result,TRUE);
                exit();
            }

            if($unit_info->minjing == $res->mjid){
                $data['is_self'] = '1';
            }else{
                $data['is_self'] = '2';
            }
            $data['unit_id'] = $res->qyid;
            $data['user_id'] = $res->mjid;

            $u = db::table('admin_users')->where('id',$res->mjid)->first();
            $data['p_city'] = $u->city;
            $data['p_area'] = $u->area;
            $data['p_org'] = $u->org_id;

            $data['mj_id'] = $unit_info->minjing;
            $data['type'] = $unit_info->type;
            $data['aqjc_type'] = $res->jc_type;
            if($res->remark != 'null'){
                $data['remark'] = $res->remark;
            }
            $data['status'] = $res->status;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $jiancha_id = DB::table('jiancha')->insertGetId($data);

            foreach ($jiancha as $k=>$v){
                $where['type_id'] = getValue($res->qyid,'units' ,'id' ,'type' );
                $where['field_name'] = $k;

                $array['jiancha_id'] = $jiancha_id;//检查id
                $array['field_content'] = db::table('unit_type_aqjc')->where($where)->value('field_content');//字段内容
                $array['field_name'] = $k;//字段名
                $array['val'] = $v;//字段值
                db::table('unit_type_aqjc_value')->insert($array);
            }

            if($jiancha_id){
                $result = array('status' => '1000',  'info' => '添加成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function self_qiye(Request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $where['user_id'] = $res->mjid;
            $re = db::table('jiancha')->where($where)->groupBy('unit_id')->pluck('unit_id');

            if(count($re) != 0){
                $unit_list = DB::table('units')->whereIn('id',$re)->get();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }

            if($unit_list){
                foreach ($unit_list as $k=>$v){
                    $list[$k]['id'] = $v->id;
                    $list[$k]['name'] = $v->name;
                    $list[$k]['pic'] = "http://nb.yntosite.com/".$v->pic;
                    $list[$k]['address'] = getValue($v->city,'area' ,'id' ,'name' ).getValue($v->area,'area' ,'id' ,'name' ).getValue($v->org_id,'area' ,'id' ,'name' ).$v->address;
                }
                $result = array('status' => '1000',  'info' => $list);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function other_qiye(Request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $where['mj_id'] = $res->mjid;
            $where['is_self'] = '2';
            $re = db::table('jiancha')->where($where)->groupBy('unit_id')->pluck('unit_id');

            if(count($re) != 0){
                $unit_list = DB::table('units')->whereIn('id',$re)->get();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }

            if($unit_list){
                foreach ($unit_list as $k=>$v){
                    $list[$k]['id'] = $v->id;
                    $list[$k]['name'] = $v->name;
                    $list[$k]['pic'] = "http://nb.yntosite.com/".$v->pic;
                    $list[$k]['address'] = getValue($v->city,'area' ,'id' ,'name' ).getValue($v->area,'area' ,'id' ,'name' ).getValue($v->org_id,'area' ,'id' ,'name' ).$v->address;
                }
                $result = array('status' => '1000',  'info' => $list);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function jiancha_list(Request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            //安全检查项
            if($res->type == 1){
                $re = db::table('jiancha')->where('unit_id',$res->qyid)->where('user_id',$res->mjid)->get();
            }
            if($res->type == 2){
                $re = db::table('jiancha')->where('unit_id',$res->qyid)->where('user_id','<>',$res->mjid)->get();
            }

            if($re){
                foreach ($re as $k=>$v){
                    $v->jiancharen = getValue($v->user_id,'admin_users' ,'id' , 'name');
                    if($v->status == 1){
                        $v->status = '合格';
                    }elseif($v->status == 2){
                        $v->status = '不合格';
                    }
                }
                $result = array('status' => '1000',  'info' => $re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function jiancha_detail(Request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            //安全检查项
            $re = db::table('jiancha')->where('id',$res->jcid)->first();
            $re->jiancharen = getValue($re->user_id,'admin_users' ,'id' , 'name');
            if($re->status == 1){
                $re->status = '合格';
            }elseif($re->status == 2){
                $re->status = '不合格';
            }

            $re->jieguo = db::table('unit_type_aqjc_value')->where('jiancha_id',$re->id)->get();

            if($re){
                $result = array('status' => '1000',  'info' => $re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }


    public function police_ph(Request $request){


           // $time = '(created_at between current_date()-7 and sysdate())';
           // $area =   '155';  
           // $newarea = 'p_org' .'='. $area;

           //  $id = 2;
           //  $res['jc_num'] = DB::select("SELECT u.rank,u.count from (SELECT user_id,count(1)as count,(@ranknum:=@ranknum+1) as rank FROM `jiancha` ,(select (@ranknum :=0) ) b  WHERE user_id  = $id AND $time AND $newarea  GROUP BY user_id) u ");//自己检查数量及排行


           //  $result = array('status' => '1000',  'message' => $res);
           //  echo json_encode($result,TRUE);
           //  exit();

           

        

        //警员自己排行
         if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));
            $uid['id'] = $res->mjid;
            $user = db::table('admin_users')->where($uid)->first();

            $id = $user->id;



            // if ($res->times == 'zhou') {

            // }


            // if ($res->city) {
            //     # code...
            // }


            if($res->type == 1){
            //获取检查排行榜

                if ($res->times == 'zhou') {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(jiancha.created_at))';
                }else if ($res->times == 'yue'){
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(jiancha.created_at))';
                }else if ($res->times) {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 365 DAY) <= date(jiancha.created_at))';
                }


                if ($res->city == 'suo') {
                    $area =  $user->org_id;
                    $ar = 'us.org_id'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'qu') {
                    $area =  $user->area;
                    $ar = 'us.area'.'='.$area.' '.'AND'.' ';
                }elseif($res->city == 'shi'){
                    $area =  $user->city;
                    $ar = 'us.city'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'sheng') {
                    $ar = '';
                }

            // SELECT u.count,u.user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM(SELECT  jiancha.user_id, us.name ,count(1)as count FROM `jiancha`  LEFT JOIN admin_users as us ON us.id = jiancha.user_id  WHERE  $ar $nt  GROUP BY user_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r

                $re['jc_num'] = DB::select("SELECT u.count,u.user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  jiancha.user_id, us.name ,count(1)as count FROM `jiancha`  LEFT JOIN admin_users as us ON us.id = jiancha.user_id  WHERE  $ar $nt GROUP BY user_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r");
            }

            if ($res->type == 2) {


                 if ($res->times == 'zhou') {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(rysh.clsj))';
                }else if ($res->times == 'yue'){
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(rysh.clsj))';
                }else if ($res->times) {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 365 DAY) <= date(rysh.clsj))';
                }


                if ($res->city == 'suo') {
                    $area =  $user->org_id;
                    $ar = 'us.org_id'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'qu') {
                    $area =  $user->area;
                    $ar = 'us.area'.'='.$area.' '.'AND'.' ';
                }elseif($res->city == 'shi'){
                    $area =  $user->city;
                    $ar = 'us.city'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'sheng') {
                    $ar = '';
                }


                // SELECT u.count,u.police_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  rysh.police_id, us.name, count(1)as count FROM `rysh`  LEFT JOIN admin_users as us ON us.id = rysh.police_id  WHERE  $ar $nt GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r

                 $re['jc_num'] = DB::select("SELECT u.count,u.police_id as user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  rysh.police_id, us.name, count(1)as count FROM `rysh`  LEFT JOIN admin_users as us ON us.id = rysh.police_id  WHERE  $ar $nt AND rysh.police_cl = 1 GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r");
            }

            if ($res->type == 3) {


                 if ($res->times == 'zhou') {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(jqsb.czsj))';
                }else if ($res->times == 'yue'){
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(jqsb.czsj))';
                }else if ($res->times) {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 365 DAY) <= date(jqsb.czsj))';
                }


                if ($res->city == 'suo') {
                    $area =  $user->org_id;
                    $ar = 'us.org_id'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'qu') {
                    $area =  $user->area;
                    $ar = 'us.area'.'='.$area.' '.'AND'.' ';
                }elseif($res->city == 'shi'){
                    $area =  $user->city;
                    $ar = 'us.city'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'sheng') {
                    $ar = '';
                }



                  // SELECT u.count,u.police_id as user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  jqsb.police_id, us.name, count(1)as count FROM `jqsb`  LEFT JOIN admin_users as us ON us.id = jqsb.police_id  WHERE  $ar $nt AND jqsb.police_cl = 1 GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r


               $re['jc_num'] = DB::select("SELECT u.count,u.police_id as user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  jqsb.police_id, us.name, count(1)as count FROM `jqsb`  LEFT JOIN admin_users as us ON us.id = jqsb.police_id  WHERE  $ar $nt AND jqsb.police_cl = 1 GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r");
            }


            if ($res->type == 4) {


                 if ($res->times == 'zhou') {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(yjfk.clsj))';
                }else if ($res->times == 'yue'){
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(yjfk.clsj))';
                }else if ($res->times) {
                    $nt = '(DATE_SUB(CURDATE(), INTERVAL 365 DAY) <= date(yjfk.clsj))';
                }


                if ($res->city == 'suo') {
                    $area =  $user->org_id;
                    $ar = 'us.org_id'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'qu') {
                    $area =  $user->area;
                    $ar = 'us.area'.'='.$area.' '.'AND'.' ';
                }elseif($res->city == 'shi'){
                    $area =  $user->city;
                    $ar = 'us.city'.'='.$area.' '.'AND'.' ';
                }elseif ($res->city == 'sheng') {
                    $ar = '';
                }



                // SELECT u.count,u.police_id as user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  yjfk.police_id, us.name, count(1)as count FROM `yjfk`  LEFT JOIN admin_users as us ON us.id = yjfk.police_id  WHERE  $ar $nt AND yjfk.police_cl = 1 GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r


                 $re['jc_num'] = DB::select("SELECT u.count,u.police_id as user_id,u.name,(CASE WHEN @rowtotal = u.count THEN @rownum WHEN @rowtotal := u.count THEN @rownum :=@rownum + 1 WHEN @rowtotal = 0 THEN  @rownum :=@rownum + 1 END) as rank FROM( SELECT  yjfk.police_id, us.name, count(1)as count FROM `yjfk`  LEFT JOIN admin_users as us ON us.id = yjfk.police_id  WHERE  $ar $nt AND yjfk.police_cl = 1 GROUP BY police_id,us.name ORDER BY count DESC) AS u,(SELECT @rownum := 0 ,@rowtotal := NULL) r");
            }





             if($re){
                $result = array('status' => '1000',  'message' => $re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '暂无数据');
                echo json_encode($result,TRUE);
                exit();
            }

         }



    }

    public function daishenhe(request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $map['minjing'] = $res->mjid;
            $map['status'] = '10';
            $re = db::table('units')->where($map)->get();


            if($re){
                $result = array('status' => '1000',  'info' => $re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function shenhe_detail(request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $map['unit_id'] = $res->id;
            $map['is_pass1'] = '1';
            $id = db::table('units_update')->where($map)->value('id');

            $re = db::table('units_update_log')->where('pid',$id)->get();

            if($re){
                $result = array('status' => '1000',  'info' => $re , 'id'=>$id ,'unit_id'=>$res->id);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function other(request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));
            $map['unit_id'] = $res->id;
            $re = db::table('units_other_log')->where($map)->get();

            if($re){
                $result = array('status' => '1000',  'info' => $re);
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '未查询到数据');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    public function shenhe(request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $data1['is_pass1'] = $res->type;
            db::table('units_update')->where('id',$res->id)->update($data1);

            if($res->type == 2){
                $data2['status'] = '20';
            }
            if($res->type == 3){
                $data2['status'] = '30';
            }
            $re = db::table('units')->where('id',$res->unit_id)->update($data2);

            if($re){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }

    //审核通过则上传到公安端
    public function upload(request $request){
        if($request->isMethod('post')){
            $res = json_decode(file_get_contents("php://input"));

            $map['id'] = $res->id;
            $map['unit_id'] = $res->unit_id;

            if($map){
                $result = array('status' => '1000',  'message' => '操作成功');
                echo json_encode($result,TRUE);
                exit();
            }else{
                $result = array('status' => '1001',  'message' => '操作失败');
                echo json_encode($result,TRUE);
                exit();
            }
        }
    }


}
